package com.gewu.pm.ui.activity.payment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TestBean;
import com.gewu.pm.ui.adapter.PaymentTermPostponeAdapter;
import com.gewu.pm.utils.eventbus.EventBean;
import com.gewu.pm.utils.eventbus.EventCode;
import com.hjq.bar.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 账期延期单列表
 */
public final class PaymentTermPostponeActivity extends AppActivity {

    private TitleBar mTitleBar;
    private EditText mSearchEdit;
    private RecyclerView mRecyclerView;
    private TextView mAddButton;
    private PaymentTermPostponeAdapter mAdapter;

    private String mSearchKeyword = "";

    public static void start(Context context) {
        Intent intent = new Intent(context, PaymentTermPostponeActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_payment_term_postpone;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.tb_title);
        mSearchEdit = findViewById(R.id.et_search);
        mRecyclerView = findViewById(R.id.rv_list);
        // ✅ 正确：使用 BottomButton 内部按钮 ID
        mAddButton = findViewById(R.id.tv_bottom_button);

        // 设置标题栏
        mTitleBar.setOnTitleBarListener(this);

        // 设置搜索框提示
        mSearchEdit.setHint("输入终端名称/编码/账期延期单单号");

        // 设置搜索监听
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mSearchKeyword = s.toString().trim();
                loadData();
            }
        });

        // 设置列表
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PaymentTermPostponeAdapter(this);
        mRecyclerView.setAdapter(mAdapter);

        // 设置按钮点击
        setOnClickListener(mAddButton);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        List<TestBean> dataList = new ArrayList<>();
        dataList.add(new TestBean("4567110867893", "落落超市"));
        dataList.add(new TestBean("4567110867894", "乐乐超市"));
        dataList.add(new TestBean("4567110867895", "哈哈超市"));
        mAdapter.setData(dataList);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_bottom_button) {
            // 跳转到新增页面
            AddPostponeActivity.start(getActivity());
        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(EventBean event) {
        if (event.getCode() == EventCode.PAYMENT_TERM_POSTPONE_ADDED) {
            // 刷新列表
            loadData();
        }
    }
}

